package com.pet.server.ext.service;

import java.util.List;

import com.pet.server.ext.ApiError;
import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.data.Article;
import com.pet.server.ext.data.Constant;
import com.pet.server.ext.data.Page;
import com.pet.server.ext.dbutil.JdbcTemplate;
import com.pet.server.ext.dbutil.Sql;


public class ArticleService {
	private JdbcTemplate jdbcTemplate = new JdbcTemplate();

	public List<Constant> queryConstant(String pcode) throws ApiError{
		Sql sql = new Sql("SELECT * FROM constant c WHERE c.pcode=? ORDER BY sort DESC");
		    sql.addParam(pcode);
		return jdbcTemplate.queryForList(sql, Constant.class);
	}
	public List<Constant> queryConstantByCate(String cate) throws ApiError{
		Sql sql = new Sql("SELECT c.* FROM constant c,cate_brand cb WHERE cb.brand=c.code AND cb.cate=?");
		    sql.addParam(cate);
		    sql.append(" ORDER BY c.sort DESC");
		    return jdbcTemplate.queryForList(sql, Constant.class);
	}
	
	public void searchArticle(Page page,String cate,String age_cate,String pet_cate) throws ApiError{
		Sql sql = new Sql("SELECT a.*,c.conts_name as cate_name FROM article a,constant c WHERE a.cate=c.code and a.state=? ");
		    sql.addParam(BaseAttribute.LOGIC_TRUE);
		    sql.appendLike("^", "AND", "a.cate", cate);
		    sql.appendLike("^", "AND", "a.age_cate", age_cate);
		    sql.appendLike("^", "AND", "a.pet_cate", pet_cate);
		    sql.append("  ORDER BY sort DESC");
		jdbcTemplate.search(page, sql, Article.class);
	}
	public List<Article> queryArticleForIndex(String cate,String pet_cate,String age_cate) throws ApiError{
		Sql sql = new Sql("SELECT a.*,c.conts_name as cate_name FROM article a,constant c WHERE a.cate=c.code and a.state=? ");
		    sql.addParam(BaseAttribute.LOGIC_TRUE);
		    sql.appendLike("^", "AND", "a.cate", cate);
		    sql.append("  ORDER BY sort DESC");
		return jdbcTemplate.queryForList(sql, Article.class,5);
	}
	public Article findArticle(Integer id) throws ApiError{
		Sql sql = new Sql("SELECT a.*FROM article a,constant c WHERE a.article_id=?");
		    sql.addParam(id);
	    return  jdbcTemplate.findObject(sql, Article.class);
	}
	
	
	
	
	
}
